package com.cxb.view;


import com.cxb.R;
import com.cxb.view.WiperSwitch.OnChangedListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class SwitchButton extends View {
	private Context context;
	private Paint paintTick;
	private Paint paintBound;
	private Paint PaintText;
	private Path path;


	private final int colorTick = Color.BLUE;
	private final int colorBound = Color.GRAY-0x111111;
	private final int colorText = Color.WHITE;
	private int w;
	private int h;
	
	
	private boolean locked = false;

	private boolean isOn = true;
	
	private OnSwitchListener onSwitchListener;
	private OnChangedListener listener;

	
	public void setOn(boolean isOn)
	{
		this.isOn = isOn;
		invalidate();
	}
	
	public void setChecked(boolean isOn)
	{
		this.isOn = isOn;
	}
	
	public boolean isOn()
	{
		return isOn;
	}

	public SwitchButton(Context context) {
		super(context);
		init(context);
	}

	public SwitchButton(Context context, AttributeSet attrs) {
		super(context, attrs);

		init(context);
	}

	public SwitchButton(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}

	private Drawable drawable_Bg;
	private Drawable drawable_on;
	private Drawable drawable_off;
	
	private float btn_w ,bg_w ;

	private void init(Context context) {
		this.context = context;
		paintTick = makePaint(colorTick, Style.FILL_AND_STROKE);
		paintBound = makePaint(colorBound, Style.FILL_AND_STROKE);
		PaintText = makePaint(colorText, Style.FILL_AND_STROKE);

		drawable_Bg = context.getResources().getDrawable(
				R.drawable.sw_on);
		drawable_on = context.getResources().getDrawable(
				R.drawable.sw_on);
		drawable_off = context.getResources().getDrawable(
				R.drawable.sw_off);

		this.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(locked)
					return;
				
				isOn = !isOn;
				if(onSwitchListener != null)
					onSwitchListener.onSwitch(isOn, SwitchButton.this);
				if(listener != null){
					listener.OnChanged(SwitchButton.this, isOn);
				}
				
				SwitchButton.this.invalidate();
			}
		});
	}
	
	
	
	public void lock()
	{
		this.locked = true;
	}
	
	public void unlock()
	{
		this.locked = false;
	}
	
	private void drawABround(Canvas canvas)
	{
		int padding = 3;
		Rect rect = new Rect(padding,padding , w-padding, h-padding);
		canvas.drawRect(rect, paintBound);
	}

	private Paint makePaint(int color, Style style) {
		Paint paint = new Paint();
		paint.setColor(color);
		paint.setStyle(style);
		return paint;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		btn_w = drawable_on.getMinimumWidth();
		bg_w = drawable_Bg.getMinimumWidth();
//		drawABround(canvas);
//		canvas.drawBitmap(drawableToBitamp(drawable_Bg),0f,0f,paintBound);
		if(isOn)
		{
			canvas.drawBitmap(drawableToBitamp(drawable_on),w - btn_w,0f,paintBound);
			Log.d("pumkid", "w "+w+" btn_w "+btn_w);
		}else if(!isOn)
		{
			canvas.drawBitmap(drawableToBitamp(drawable_off),w - btn_w,0f,paintBound);
		}
	}


	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		w = this.getMeasuredWidth();
		h = this.getMeasuredHeight();
		h = drawable_on.getMinimumHeight();
		setMeasuredDimension(widthMeasureSpec, h);
	}

	private Bitmap drawableToBitamp(Drawable drawable) {
		BitmapDrawable bd = (BitmapDrawable) drawable;
		return bd.getBitmap();
	}
	
	
	public void setOnSwitchListener(OnSwitchListener onSwitchListener) {
		this.onSwitchListener = onSwitchListener;
	}

	public void setOnChangedListener(OnChangedListener listener){
		this.listener = listener;
	}
	  /**
     * 回调接口
     * @author len
     *
     */
	public interface OnChangedListener {
		public void OnChanged(View wiperSwitch, boolean checkState);
	}



	public interface OnSwitchListener
	{
		public void onSwitch(boolean isOn, View view);
	}
}
